package com.example.friend.mvp.ui.adapter;

import android.app.Activity;
import android.net.Uri;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.friend.R;
import com.example.friend.mvp.model.CircleModel;
import com.example.friend.mvp.ui.util.GlideSimpleLoader;
import com.example.friend.mvp.ui.weight.CustomPopupWindow;
import com.github.ielse.imagewatcher.ImageWatcher;
import com.github.ielse.imagewatcher.ImageWatcherHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: admins
 * date:    2019/6/26 17:51
 * description: 朋友圈适配器
 */
public class CircleAdapter extends BaseQuickAdapter<CircleModel, BaseViewHolder> implements CustomPopupWindow.ViewInterface  {
    private TextView tvCommon, tvLike;
    private RecyclerView rvChild;
    private ImageView imageView;
    public ImageWatcherHelper iwHelper;
    private CustomPopupWindow popupWindow;

    public CircleAdapter(@Nullable List<CircleModel> data) {
        super(R.layout.item_circle, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, CircleModel item) {
        if (item == null) {
            return;
        }
        iwHelper = ImageWatcherHelper.with(((Activity)mContext),new GlideSimpleLoader()) // 一般来讲， ImageWatcher 需要占据全屏的位置
                .setErrorImageRes(R.mipmap.error_picture); // 配置error图标 如果不介意使用lib自带的图标，并不一定要调用这个API

        tvCommon = helper.getView(R.id.tv_common);
        tvLike = helper.getView(R.id.tv_like);
        imageView = helper.getView(R.id.iv_photo);
        rvChild = helper.getView(R.id.rv_photo);

        helper.setText(R.id.tv_name, item.getName());
        helper.setText(R.id.tv_content, item.getContent());
        //评论
        helper.addOnClickListener(R.id.tv_common);
        //删除
        helper.addOnClickListener(R.id.tv_delete);
        ChildCircleAdapter childCircleAdapter = new ChildCircleAdapter(item.getList());
        rvChild.setLayoutManager(new LinearLayoutManager(mContext));
        rvChild.setAdapter(childCircleAdapter);

        childCircleAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.iv_user) {
                    final List<String> longPictureList = new ArrayList<>();
                    longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=87e5c3def99fdad3e356263114f11f0b&imgtype=0&src=http%3A%2F%2Fthumb11.jfcdns.com%2Fthumb%2F2017-11%2Fbce5a02c4551db77_600_400.jpeg");
                    longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=a0dbe4cb9420b031669693f2d170aaae&imgtype=0&src=http%3A%2F%2Fthumb10.jfcdns.com%2Fthumb%2F2017-11%2Fbce5a02c45735cc6_600_400.jpeg");
                    longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355543&di=a1b77f149342811754180a7b88382e13&imgtype=0&src=http%3A%2F%2Fgss0.bdstatic.com%2F7LsWdDW5_xN3otebn9fN2DJv%2Fdoc%2Fpic%2Fitem%2Ff31fbe096b63f62442fe1e3b8b44ebf81a4ca362.jpg");
                    longPictureList.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536811355545&di=2436088f354f1a8355ea8db0a82592e0&imgtype=0&src=http%3A%2F%2Fgss0.bdstatic.com%2F7LsWdDW5_xN3otebn9fN2DJv%2Fdoc%2Fpic%2Fitem%2F3801213fb80e7bec392a30d9232eb9389b506b11.jpg");
                    final int initPosition = 0;
                    final SparseArray<ImageView> mappingViews = new SparseArray<>();
                    mappingViews.put(initPosition, (ImageView) view);
                    iwHelper.show(initPosition, mappingViews, converts(longPictureList));
                    iwHelper.setOnStateChangedListener(new ImageWatcher.OnStateChangedListener() {
                        @Override
                        public void onStateChangeUpdate(ImageWatcher imageWatcher, ImageView clicked, int position, Uri uri, float animatedValue, int actionTag) {

                        }

                        @Override
                        public void onStateChanged(ImageWatcher imageWatcher, int position, Uri uri, int actionTag) {

                        }
                    });
                    iwHelper.setOnPictureLongPressListener(new ImageWatcher.OnPictureLongPressListener() {
                        @Override
                        public void onPictureLongPress(ImageView v, Uri uri, int pos) {
                            showSharePopup(v);
                        }
                    });
                }
            }
        });
    }

    private void showSharePopup(View view) {
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        popupWindow = new CustomPopupWindow.Builder(mContext)
                .setView(R.layout.dialog_share)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
                .setAnimationStyle(R.style.anim_push_bottom)
                .setBackGroundLevel(0.5f)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
    }


    public List<Uri> converts(List<String> data) {
        List<Uri> list = new ArrayList<>();
        for (String d : data) list.add(Uri.parse(d));
        return list;
    }


    @Override
    public void getChildView(View view, int layoutResId) {

    }
}
